package ${t.packagePath}.config;

import cn.dev33.satoken.stp.StpInterface;
import org.springframework.stereotype.Component;
import ${t.packagePath}.model.*;
import ${t.packagePath}.service.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限验证接口扩展
 */
@Component    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private ${t.userTableName}Service ${t.userTableName?uncap_first}Service;
    @Resource
    private ${t.userRoleTableName}Service ${t.userRoleTableName?uncap_first}Service;
    @Resource
    private ${t.rolePermissionTableName}Service ${t.rolePermissionTableName?uncap_first}Service;
    @Resource
    private ${t.roleMenuTableName}Service ${t.roleMenuTableName?uncap_first}Service;
    @Resource
    private ${t.menuTableName}Service ${t.menuTableName?uncap_first}Service;
    @Resource
    private ${t.permissionTableName}Service ${t.permissionTableName?uncap_first}Service;
    @Resource
    private ${t.roleTableName}Service ${t.roleTableName?uncap_first}Service;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        //多账号体系下的权限获取
        if (Stp${t.userTableName}Util.TYPE.equals(loginType)) {
            List<String> list = new ArrayList<>();
            ${t.userTableName} ${t.userTableName?uncap_first} = ${t.userTableName?uncap_first}Service.getById(loginId.toString());
            List<${t.userRoleTableName}> roles = ${t.userRoleTableName?uncap_first}Service.lambdaQuery().eq(${t.userRoleTableName}::getUserId, ${t.userTableName?uncap_first}.getId()).list();
            List<Long> roleIds = roles.stream().map(${t.userRoleTableName}::getRoleId).collect(Collectors.toList());
            //无角色
            if (roleIds.size() == 0) {
                return list;
            }
            List<${t.rolePermissionTableName}> rolePermissions = ${t.rolePermissionTableName?uncap_first}Service.lambdaQuery().in(${t.rolePermissionTableName}::getRoleId, roleIds).list();
            List<Long> permissionIds = rolePermissions.stream().map(${t.rolePermissionTableName}::getPermissionId).collect(Collectors.toList());
            //无权限
            if (permissionIds.size() == 0) {
                return list;
            }
            List<${t.permissionTableName}> permissions = ${t.permissionTableName?uncap_first}Service.lambdaQuery().in(${t.permissionTableName}::getId, permissionIds).list();
            return permissions.stream().map(${t.permissionTableName}::getCode).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        //多账号体系下的角色获取
        if (Stp${t.userTableName}Util.TYPE.equals(loginType)) {
            List<String> list = new ArrayList<>();
            ${t.userTableName} ${t.userTableName?uncap_first} = ${t.userTableName?uncap_first}Service.getById(loginId.toString());
            List<${t.userRoleTableName}> userRoles = ${t.userRoleTableName?uncap_first}Service.lambdaQuery().eq(${t.userRoleTableName}::getUserId, ${t.userTableName?uncap_first}.getId()).list();
            List<Long> roleIds = userRoles.stream().map(${t.userRoleTableName}::getRoleId).collect(Collectors.toList());
            //无角色
            if (roleIds.size() == 0) {
                return list;
            }
            List<${t.roleTableName}> roles = ${t.roleTableName?uncap_first}Service.lambdaQuery().in(${t.roleTableName}::getId, roleIds).list();
            return roles.stream().map(${t.roleTableName}::getName).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 获取当前用户菜单
     */
    public List<${t.menuTableName}> getMenuList(Object loginId, String loginType) {
        // 多账号体系下的菜单获取
        if (Stp${t.userTableName}Util.TYPE.equals(loginType)) {
            List<${t.menuTableName}> list = new ArrayList<>();
            ${t.userTableName} ${t.userTableName?uncap_first} = ${t.userTableName?uncap_first}Service.getById(loginId.toString());
            List<${t.userRoleTableName}> roles = ${t.userRoleTableName?uncap_first}Service.lambdaQuery().eq(${t.userRoleTableName}::getUserId, ${t.userTableName?uncap_first}.getId()).list();
            List<Long> roleIds = roles.stream().map(${t.userRoleTableName}::getRoleId).collect(Collectors.toList());
            if (roleIds.size() == 0) {
                return list;
            }
            List<${t.roleMenuTableName}> roleMenuList = ${t.roleMenuTableName?uncap_first}Service.lambdaQuery().in(${t.roleMenuTableName}::getRoleId, roleIds).list();
            List<Long> menuIds = roleMenuList.stream().map(${t.roleMenuTableName}::getMenuId).collect(Collectors.toList());
            if (menuIds.size() == 0) {
                return list;
            }
            List<${t.menuTableName}> uMenus = ${t.menuTableName?uncap_first}Service.lambdaQuery().in(${t.menuTableName}::getId, menuIds).list();
            return uMenus;
        }
        return null;
    }
}